/**
 * [38] 外观数列
 *
 * @format
 * @lc app=leetcode.cn id=38 lang=javascript
 */

// @lc code=start
/**
 * @param {number} n
 * @return {string}
 */
var countAndSay = function (n) {
    if (n == 1) return "1"
    let s = countAndSay(n - 1)
    let [ans, last, count] = ["", s[0], 1]
    for (let c of s.slice(1)) {
        if (c == last) count++
        else {
            ans += `${count}${last}`
            last = c
            count = 1
        }
    }
    return ans + `${count}${last}`
}
// @lc code=end
